<?php
/**
 * Author:xsc
 * Time:2023/10/10 10:37
 */

namespace App\Repository;

use App\Models\SpecialPrivilegeUgcOrder;

class SpecialPrivilegeUgcOrderRepo extends BaseRepo
{

    /**
     * 新增/更新订单
     * @param array $data
     * @param int $id
     * @return int
     * Author:xsc
     * Time:2023/10/10 10:40
     */
    public function save(array $data, int $id = 0)
    {
        if ($id) {
            return SpecialPrivilegeUgcOrder::query()->where('id', $id)->update($data);
        } else {
            return SpecialPrivilegeUgcOrder::query()->insertGetId($data);
        }
    }

    /**
     * 根据第三方订单号更新订单
     * @param array $data
     * @param string $third_no
     * @return int
     * Author:xsc
     * Time:2023/10/16 11:00
     */
    public function updateByThirdNo(array $data, string $third_no)
    {
        return SpecialPrivilegeUgcOrder::query()->where('thirdNo', $third_no)->update($data);
    }

    /**
     * 查询未结束的赋能订单
     * @param int $goods_id
     * @return array
     * Author:xsc
     * Time:2023/10/18 15:13
     */
    public function getUnfinishedOrder(int $goods_id)
    {
        return SpecialPrivilegeUgcOrder::query()->where('goods_id', $goods_id)->whereNotIn('status', [2, 5, 6])->get()->toArray();
    }

    /**
     * 列表
     * @param array $params
     * @return array
     * Author:xsc
     * Time:2023/10/10 16:48
     */
    public function list(array $params)
    {
        $where = [];
        if (isset($params['ordersn'])) {
            $where[] = ['ordersn', '=', $params['ordersn']];
        }
        if (isset($params['status'])) {
            $where[] = ['status', '=', $params['status']];
        }
        if (isset($params['special_id'])) {
            $where[] = ['special_id', '=', $params['special_id']];
        }
        if (isset($params['goods_id'])) {
            $where[] = ['goods_id', '=', $params['goods_id']];
        }
        if (isset($params['mer_id'])) {
            $where[] = ['mer_id', '=', $params['mer_id']];
        }
        if (isset($params['from_member_id'])) {
            $where[] = ['from_member_id', '=', $params['from_member_id']];
        }
        if (isset($params['coupon_code'])) {
            $where[] = ['coupon_code', '=', $params['coupon_code']];
        }
        if (isset($params['thirdNo'])) {
            $where[] = ['thirdNo', '=', $params['thirdNo']];
        }

        $res = SpecialPrivilegeUgcOrder::query()
            ->where($where)
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
        return $res;
    }

    /**
     * 根据订单号获取订单
     * @param string $order_sn
     * @return array
     * Author:xsc
     * Time:2023/10/19 17:52
     */
    public function getByOrderSn(string $order_sn)
    {
        $res = SpecialPrivilegeUgcOrder::query()->where('ordersn', $order_sn)->first();
        return $res ? $res->toArray() : [];
    }

    /**
     * 根据id获取订单
     * @param int $id
     * @return array
     * Author:xsc
     * Time:2023/10/23 13:11
     */
    public function getById(int $id)
    {
        $res = SpecialPrivilegeUgcOrder::query()->where('id', $id)->first();
        return $res ? $res->toArray() : [];
    }

}
